﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;

public partial class Mod_FlatManger_frm_CorporationSet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //this.GetCorporationSets();
        }
        else
        {
            this.lblJs.Text = "";//清空JS脚本
        }

   //     this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
   //     this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
   //     this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_CorporationSet = new ClsPropertyDAL();
        DataSet dst = cls_CorporationSet.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindCorporationSetsByDataView(dst, intPageIndex);
        }
    }

    public void AddCorporationSetInfo(object sender, EventArgs e)
    {
        ClsCorporationDAL cls_CorporationSet = new ClsCorporationDAL();

        string strcomp_ID = this.txt1.Value.Trim();//编号
        string strcomp_Name = this.txt2.Text.Trim();//企业简称
        string strcomp_desc = this.txt3.Text.Trim();//企业全称

        //string strcomp_eng = this.txt4.Text.Trim();//英文名称
        string strcomp_eng = "";//英文名称
        string strcomp_boss = this.txt5.Text.Trim();//法人代表
        string strcomp_tel1 = this.txt6.Text.Trim();//电话一
        string strcomp_tel2 = this.txt7.Text.Trim();//电话二
        string strcomp_fax1 = this.txt8.Text.Trim();//传真一
        string strcomp_fax2 = this.txt9.Text.Trim();//传真二
        string strcomp_address = this.txt10.Text.Trim();//详细地址
        string strcomp_post = this.txt11.Text.Trim();//邮编
        string strcomp_email = this.txt12.Text.Trim();//邮箱
        string strcomp_website = this.txt13.Text.Trim();//网址
        string strcomp_bank = this.txt14.Text.Trim();//开户银行
        string strcomp_accno = this.txt15.Text.Trim();//银行帐号
        string strcomp_taxno = this.txt16.Text.Trim();//税号     
        int icomp_statu = int.Parse(this.txt23.SelectedValue);//状态
        string strcomp_Remark = this.txt22.Text.Trim();//备注

        Boolean bolIsCussee = cls_CorporationSet.AddCorporation(strcomp_ID, strcomp_Name, strcomp_desc,
            strcomp_eng, strcomp_boss, strcomp_tel1, strcomp_tel2, strcomp_fax1, strcomp_fax2, strcomp_address, strcomp_post, strcomp_email,
            strcomp_website, strcomp_bank,strcomp_accno,strcomp_taxno,
            "思捷用户",icomp_statu,strcomp_Remark);

        if (bolIsCussee)
        {
            this.GetCorporationSets();//重新绑定

            StringBuilder strJs = new StringBuilder();
            strJs.Append("<script>document.getElementById('trAdd').style.display='block';");
            strJs.Append("var dt=new Date();var y=dt.getYear();var m=dt.getMonth();var d=dt.getDay();var h=dt.getHours();var mi=dt.getMinutes();var s=dt.getSeconds();var ct=y+''+m+''+d+''+h+''+mi+''+s;");
            strJs.Append("document.getElementById('txt1').value = 'C_'+ct;");
            strJs.Append("document.getElementById('txt2').value = '';");
            strJs.Append("document.getElementById('txt3').value = '';");
            //strJs.Append("document.getElementById('txt4').value = '';");
            strJs.Append("document.getElementById('txt5').value = '';");
            strJs.Append("document.getElementById('txt6').value = '';");
            strJs.Append("document.getElementById('txt7').value = '';");
            strJs.Append("document.getElementById('txt8').value = '';");
            strJs.Append("document.getElementById('txt9').value = '';");
            strJs.Append("document.getElementById('txt10').value = '';");
            strJs.Append("document.getElementById('txt11').value = '';");
            strJs.Append("document.getElementById('txt12').value = '';");
            strJs.Append("document.getElementById('txt13').value = '';");
            strJs.Append("document.getElementById('txt14').value = '';");
            strJs.Append("document.getElementById('txt15').value = '';");
            strJs.Append("document.getElementById('txt16').value = '';");
            strJs.Append("document.getElementById('txt22').value = '';");
            strJs.Append("document.getElementById('" + this.txt2.ClientID + "').select();");
            strJs.Append("document.getElementById('" + this.txt2.ClientID + "').focus()</script>");
            this.lblJs.Text = strJs.ToString();
        }
        else
        {
            this.lblJs.Text = "<script>alert('新增企业信息失败，请重试！')</script>";
        }
    }

    public void UpDateCorporationSetInfo(object sender, EventArgs e)
    {
        ClsCorporationDAL cls_CorporationSet = new ClsCorporationDAL();
        ImageButton ibtn = (ImageButton)sender;

        int intCommandName = Int32.Parse(ibtn.CommandName);

        if (intCommandName == 1)//确认
        {
            int intCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intCount; i++)
            {
                CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkSelect");

                if (chkBox.Checked)
                {
                    TextBox txtBox = (TextBox)this.dgrdResult.Items[i].FindControl("comp_id");

                    Boolean bolIsCussee = cls_CorporationSet.AuditStatCorporation(txtBox.Text.Trim(), intCommandName, "思捷用户");

                    if (!bolIsCussee)
                    {
                        this.lblJs.Text = "<script>alert('确认企业信息失败，请重试！')</script>";
                    }
                }
            }

            this.GetCorporationSets();//重新绑定
        }
        else//取消确认，编辑，取消编辑，保存编辑
        {
            int intIndex = Int32.Parse(ibtn.CommandArgument);

            CheckBox chk = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkSelect");
            TextBox comp_id = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_id");
            TextBox comp_name = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_name");

            TextBox comp_desc = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_desc");
            //TextBox comp_eng = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_eng");
            TextBox comp_boss = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_boss");
            TextBox comp_tel1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_tel1");
            TextBox comp_tel2 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_tel2");
            TextBox comp_fax1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_fax1");
            TextBox comp_fax2 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_fax2");
            TextBox comp_address = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_address");
            TextBox comp_post = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_post");
            TextBox comp_email = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_email");
            TextBox comp_website = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_website");
            TextBox comp_bank = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_bank");
            TextBox comp_accno = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_accno");
            TextBox comp_taxno = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_taxno");

            TextBox comp_statut = (TextBox)this.dgrdResult.Items[intIndex].FindControl("comp_statut");
            DropDownList comp_statu = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("comp_statu");
            TextBox remark = (TextBox)this.dgrdResult.Items[intIndex].FindControl("remark");
            ImageButton ibtnEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnEdit");
            ImageButton ibtnCan = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnCan");
            ImageButton ibtnSer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnSer");
            ImageButton ibtnOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOAudit");

            if (intCommandName == 0)//取消确认
            {
                Boolean bolIsCussee = cls_CorporationSet.AuditStatCorporation(comp_id.Text.Trim(), intCommandName, "思捷用户");

                if (bolIsCussee)
                {
                    this.GetCorporationSets();//重新绑定
                }
                else
                {
                    this.lblJs.Text = "<script>alert('取消确认企业信息失败，请重试！')</script>";
                }
            }
            else if (intCommandName == 2)//编辑
            {
                chk.Visible = ibtnEdit.Visible = comp_statut.Visible = false;
                ibtnCan.Visible = ibtnSer.Visible = comp_statu.Visible = true;
                //comp_name.ReadOnly = comp_desc.ReadOnly = remark.ReadOnly = false;
                comp_name.ReadOnly = comp_desc.ReadOnly = comp_boss.ReadOnly = comp_tel1.ReadOnly = comp_tel2.ReadOnly = false;
                comp_fax1.ReadOnly = comp_fax2.ReadOnly = comp_address.ReadOnly = comp_post.ReadOnly = comp_email.ReadOnly = false;
                comp_website.ReadOnly = comp_bank.ReadOnly = comp_accno.ReadOnly = comp_taxno.ReadOnly = remark.ReadOnly = false;

                comp_name.BorderWidth = comp_desc.BorderWidth = comp_boss.BorderWidth = comp_tel1.BorderWidth = comp_tel2.BorderWidth = 1;
                comp_fax1.BorderWidth = comp_fax2.BorderWidth = comp_address.BorderWidth = comp_post.BorderWidth = comp_email.BorderWidth = 1;
                comp_website.BorderWidth = comp_bank.BorderWidth = comp_accno.BorderWidth = comp_taxno.BorderWidth = remark.BorderWidth = 1;

                //让第一个文本框获取焦点
                this.lblJs.Text = "<script>document.getElementById('" + comp_name.ClientID + "').select();document.getElementById('" + comp_name.ClientID + "').focus()</script>";
            }
            else if (intCommandName == 3)//取消编辑
            {
                chk.Visible = ibtnEdit.Visible = comp_statut.Visible = true;
                ibtnCan.Visible = ibtnSer.Visible = comp_statu.Visible = false;
                comp_name.ReadOnly = remark.ReadOnly = true;
                comp_name.BorderWidth = 0;

                this.lblJs.Text = "";
                this.GetCorporationSets();//重新绑定
            }
            else//保存编辑
            {
                Boolean bolIsCussee = cls_CorporationSet.UpDateCorporation(comp_id.Text.Trim(), comp_name.Text.Trim(),
                                comp_desc.Text.Trim() , "" , comp_boss.Text.Trim() , comp_tel1.Text.Trim() , comp_tel2.Text.Trim() 
                                ,comp_fax1.Text.Trim() , comp_fax2.Text.Trim() , comp_address.Text.Trim() , comp_post.Text.Trim() , comp_email.Text.Trim(), 
                                comp_website.Text.Trim() , comp_bank.Text.Trim() , comp_accno.Text.Trim() , comp_taxno.Text.Trim(),int.Parse(comp_statu.SelectedValue),
                                remark.Text.Trim());

                if (bolIsCussee)
                {
                    this.lblJs.Text = "";
                    this.GetCorporationSets();//重新绑定
                }
                else
                {
                    this.lblJs.Text = "<script>alert('修改企业信息失败，请重试！')</script>";
                }
            }
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteCorporationSetInfo(intCommandName);
    }

    private void DeleteCorporationSetInfo(int CommandName)
    {
        ClsCorporationDAL cls_CorporationSet = new ClsCorporationDAL();

        if (CommandName == 1 || CommandName == -1)
        {
            int intCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intCount; i++)
            {
                CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkSelect");

                if (chkBox.Checked)
                {
                    TextBox txtBox = (TextBox)this.dgrdResult.Items[i].FindControl("comp_id");

                    Boolean bolIsCussee = cls_CorporationSet.DeleteCorporation(txtBox.Text.Trim(), CommandName);

                    if (!bolIsCussee)
                    {
                        this.lblJs.Text = "<script>alert('作废企业信息失败，请重试！')</script>";
                    }
                }
            }
        }

        this.GetCorporationSets();//重新绑定
    }

    private void GetCorporationSets()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsCorporationDAL cls_CorporationSet = new ClsCorporationDAL();
            //DataSet dst = cls_CorporationSet.GetCorporations("00", strUserID, "DS000101");
            DataSet dst = null;
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindCorporationSetsByDataView(dst, 0);
            }
            else
            {
                this.lblRsCounts.Text = "共0条记录";
                this.lblPageCounts.Text = "共0页";
                this.lblCurrPage.Text = "当前第0页";
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='index.aspx'</script>";
        }
    }

    private void BindCorporationSetsByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(78, 78, 78);

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
        this.dgrdResult.DataBind();
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
       
        string Strid = this.txtwh_id.Text.Trim();
        string Strno = this.txtim_no.Text.Trim();

        if(true)
            //(Strid != "" && Strno = "")
        {
            ClsStockQueryDAL cls_CorporationSet = new ClsStockQueryDAL();
            //if (Strid == "" & Strno == "")
            //    this.lblJs.Text = "<script>alert('请输入查询条件！')</script>";
            DataSet dst = cls_CorporationSet.SearchStockQuery(Strid, Strno,this.CheckBox1.Checked);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindCorporationSetsByDataView(dst, 0);
            }
            else
            {
               this.lblJs.Text = "<script>alert('搜索结果为空！')</script>";
               this.BindCorporationSetsByDataView(dst, 0);
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请输入搜索关键字！')</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_CorporationSet = new ClsPropertyDAL();
        DataSet dst = cls_CorporationSet.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindCorporationSetsByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
           // Boolean Comp_statu = Boolean.Parse(e.Item.Cells[1].Text.Trim());
           // string strIs_audit = e.Item.Cells[2].Text.Trim();

            CheckBox chkbox = (CheckBox)e.Item.FindControl("chkSelect");
            TextBox remark = (TextBox)e.Item.FindControl("remark");
            ImageButton ibtnEdit = (ImageButton)e.Item.FindControl("ibtnEdit");
            ImageButton ibtnCan = (ImageButton)e.Item.FindControl("ibtnCan");
            ImageButton ibtnSer = (ImageButton)e.Item.FindControl("ibtnSer");
            ImageButton ibtnOAudit = (ImageButton)e.Item.FindControl("ibtnOAudit");
            TextBox comp_statut = (TextBox)e.Item.FindControl("comp_statut");
            DropDownList comp_statu = (DropDownList)e.Item.FindControl("comp_statu");
            System.Web.UI.WebControls.Image is_audit = (System.Web.UI.WebControls.Image)e.Item.FindControl("is_audit");
//            comp_statu.SelectedIndex = (!Comp_statu) ? 0 : 1;
            comp_statut.Text = comp_statut.ToolTip = comp_statu.SelectedItem.Text;

            //修改按回车提交
            if (remark.ReadOnly)
            {
                remark.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnSer.ClientID + "').click();}");
            }

         
        }
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteCorporationSetInfo(intCommandName);
    }
}
